package com.tang.logger.pusher;

import com.tang.logger.domain.LogMessage;
import org.springframework.scheduling.concurrent.CustomizableThreadFactory;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author Tang
 * @classname AbstractLogPusher
 * @description [  ]
 * @date 2021/7/25 11:08
 */
public abstract class AbstractLogPusher implements LogPusher{

    private ExecutorService threadPool;

    public AbstractLogPusher(String namePrefix) {
        threadPool = new ThreadPoolExecutor(2, 3, 100, TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<>(10),new CustomizableThreadFactory(namePrefix));
    }

    @Override
    public void push(LogMessage logMessage) {
        threadPool.execute(() -> {
            push0(logMessage);
        });
    }

    /**
     * 实际推送日志消息处理逻辑
     * @param logMessage
     */
    protected abstract void push0(LogMessage logMessage);
}